package org.example.tms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import org.example.tms.config.AjaxResult;
import org.example.tms.mapper.DeptMapper;
import org.example.tms.mapper.EmpExprMapper;
import org.example.tms.mapper.EmpMapper;
import org.example.tms.pojo.Dept;
import org.example.tms.pojo.Emp;
import org.example.tms.pojo.EmpExpr;
import org.example.tms.pojo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
public class EmpController {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @GetMapping("/emps")
    public AjaxResult empList(String name, String gender,
                              //参数转化
                              @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                              @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,
                              @RequestParam(defaultValue = "1",name="page") Integer pageNum,
                              @RequestParam(defaultValue = "1") Integer pageSize
                              ) {
        Page<Emp> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Emp> queryWrapper = new QueryWrapper<>();
        //querWrapper执行sql语句
        queryWrapper.like(name != null && name.length() > 0, "name", name);
        queryWrapper.eq(gender != null&& !gender.trim().isEmpty(),"gender",gender);
        queryWrapper.between(begin != null&&end!=null,"entry_date",begin,end);
        //Mapper执行
        empMapper.selectPage(page, queryWrapper);

        //foreach用法
        for (Emp emp : page.getRecords()) {
            Dept dept = deptMapper.selectById(emp.getDeptId());
            emp.setDeptName(dept.getName());
        }

        PageBean pageBean = new PageBean();
        pageBean.setRows(page.getRecords());
        pageBean.setTotal(page.getTotal());

        return AjaxResult.success(pageBean);

    }
    @DeleteMapping("/emps")
    public AjaxResult delete(@RequestParam List<Integer> ids) {
        empMapper.deleteBatchIds(ids);
        return AjaxResult.success();
    }
    @PostMapping("/emps")
    public AjaxResult addEmp(@RequestBody Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        //默认初始密码123456
        emp.setPassword("123456");
        empMapper.insert(emp);
        //工作经历的储存
        List<EmpExpr> exprList = emp.getExprList();
        //插入emp_id(工作经历表)
        exprList.forEach(expr -> {
            expr.setEmpId(emp.getId());
            empExprMapper.insert(expr);
        });

        return AjaxResult.success();
    }
    @GetMapping("/emps/{id}")
    public AjaxResult getEmpById(@PathVariable Integer id) {
        //员工信息的查询
        Emp emp = empMapper.selectById(id);

        //查询工作经历
        QueryWrapper<EmpExpr> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("emp_id", emp.getId());
        List<EmpExpr> exprList = empExprMapper.selectList(queryWrapper);
        //封装
        emp.setExprList(exprList);
        return AjaxResult.success(emp);
    }
    @PutMapping("/emps")
    public AjaxResult updateEmp(@RequestBody Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        //删除经历 querWrapper条件构造器
        QueryWrapper<EmpExpr> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("emp_id",emp.getId());
        empExprMapper.delete(queryWrapper);

        List<EmpExpr> exprList = emp.getExprList();
        if(exprList!=null&&exprList.size()>0){
            for (EmpExpr expr : exprList) {
                expr.setEmpId(emp.getId());
                empExprMapper.insert(expr);
            }
        }
        return AjaxResult.success();
    }
    @GetMapping("/emps/list")
    public AjaxResult empList(){
        List<Emp> emps = empMapper.selectList(null);
        return  AjaxResult.success(emps);
    }
}
